package leetcode_tag.二分查找;

import java.util.Arrays;

public class LeetCode34 {

    public static void main(String[] args) {
        LeetCode34 leetCode34 = new LeetCode34();
        System.out.println(Arrays.toString(leetCode34.searchRange(new int[]{5, 7, 7, 8, 8, 10}, 8)));
    }

    public int[] searchRange(int[] nums, int target) {
        if (nums.length == 1)
            return nums[0] == target ? new int[]{0, 0} : new int[]{-1, -1};
        int l = 0, r = nums.length - 1;
        int[] ans = new int[2];
        while (l <= r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] < target) {
                ans[0] = mid;
                l = mid + 1;
            } else
                r = mid - 1;
        }
        l = 0;
        r = nums.length - 1;
        while (l <= r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] <= target) {
                l = mid + 1;
            } else {
                ans[1] = mid;
                r = mid - 1;
            }
        }
        ans[0] += 1;
        ans[1] -= 1;
        return ans;
    }

}
